package com.d3works.apps.project.web;

import com.d3works.apps.project.service.ProjectActivityDashboardService;
import com.d3works.exception.service.TraceBackService;
import com.d3works.inject.Beans;
import com.d3works.rpc.ActionRequest;
import com.d3works.rpc.ActionResponse;
import com.google.inject.Singleton;
import java.time.LocalDate;

@Singleton
public class ProjectActivityDashboardController {

  public void getData(ActionRequest request, ActionResponse response) {

    try {
      LocalDate todayDate = LocalDate.now();
      Long projectId = (Long) request.getContext().get("id");
      response.setValues(
          Beans.get(ProjectActivityDashboardService.class)
              .getData(todayDate.minusDays(29), todayDate, projectId));
    } catch (Exception e) {
      TraceBackService.trace(response, e);
    }
  }

  public void getPreviousData(ActionRequest request, ActionResponse response) {

    try {
      String startDate = request.getContext().get("startDate").toString();
      Long projectId = (Long) request.getContext().get("id");
      response.setValues(
          Beans.get(ProjectActivityDashboardService.class).getPreviousData(startDate, projectId));
    } catch (Exception e) {
      TraceBackService.trace(response, e);
    }
  }

  public void getNextData(ActionRequest request, ActionResponse response) {
    try {
      String endDate = request.getContext().get("endDate").toString();
      Long projectId = (Long) request.getContext().get("id");
      response.setValues(
          Beans.get(ProjectActivityDashboardService.class).getNextData(endDate, projectId));
    } catch (Exception e) {
      TraceBackService.trace(response, e);
    }
  }
}
